home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / NTUMIN10.ARJ / ADJACENT.C < prev    next >
C/C++ Source or Header  |  1992-03-12  |  4KB  |  124 lines

  1. /****************************************************************************
  2.  *
  3.  *    Program Name : ADJACENT.C
  4.  *
  5.  *    Written By : Eng-Huat Ong and Kian-Mong Low.
  6.  *
  7.  *    This program generate the possible adjacent terms given the minterm.
  8.  *     It then computes the adjacency of the minterm based on the possible
  9.  *    adjacent terms generated.
  10.  *
  11.  *    Returns pointer to an array containing :
  12.  *        1.  no. of variables, n
  13.  *        2.  adjacency, adj
  14.  *        3.  no. of bytes/minterm, nspm
  15.  *        4.  the given minterm
  16.  *        5.  all adjacent terms
  17.  *
  18.  * --------------------------------------------------------------------------
  19.  *    Copyright (c) 1992. All Rights Reserved. Nanyang Technological
  20.  *    University.
  21.  *
  22.  *    You are free to use, copy and distribute this software and its
  23.  *    documentation providing that:
  24.  *
  25.  *        NO FEE IS CHARGED FOR USE, COPYING OR DISTRIBUTION.
  26.  *
  27.  *        IT IS NOT MODIFIED IN ANY WAY.
  28.  *
  29.  *        THE COPYRIGHT NOTICE APPEAR IN ALL COPIES.
  30.  *
  31.  *    This program is provided "AS IS" without any warranty, expressed or
  32.  *    implied, including but not limited to fitness for any particular
  33.  *    purpose.
  34.  *
  35.  *    If you find NTUMIN fast, easy, and useful, a note or comment would be
  36.  *    appreciated. Please send to:
  37.  *
  38.  *        Boon-Tiong Tan or Othman Bin Ahmad
  39.  *        School of EEE
  40.  *        Nanyang Technological University
  41.  *        Nanyang Avenue
  42.  *        Singapore 2263
  43.  *        Republic of Singapore
  44.  *
  45.  ***************************************************************************/
  46.  
  47. #include <stdio.h>
  48. #include <string.h>
  49. #include <stdlib.h>
  50.  
  51. unsigned char   *adjacent(temp, a, limit)
  52. unsigned char     *a, *temp, limit;
  53.  
  54. {
  55.    unsigned short  m;
  56.    unsigned char   *c, *adjterm, n, adj, i, j, nspm;
  57.    char            test;
  58.  
  59.  
  60.    n    = *a;                           /* no. of variables */
  61.    nspm = *(a+3);                       /* no. of storage per minterm */
  62.    m  = *(a+2)<<8 | *(a+1);             /* no. of minterms in a */
  63.  
  64.    c = (unsigned char *)  malloc(nspm+4);       /* 4 bytes for header */
  65.    if (c == 0)
  66.       {
  67.      printf("Out of memory -- ADJACENT, *c\n");
  68.      printf("Program terminated - 1\n");
  69.      exit(0);
  70.       }
  71.  
  72.    *c = n;                                      /* no. of variables */
  73.    *(c+3) = nspm;                               /* no. of bytes/minterm */
  74.  
  75.    memcpy((c+4), temp, nspm);           /* minterm added as 1st term in c */
  76.  
  77.    adjterm = (unsigned char *)  malloc(nspm+1);     /* temporary storage */
  78.    if (adjterm == 0)
  79.       {
  80.      printf("Out of memory -- ADJACENT, *c\n");
  81.      printf("Program terminated - 2\n");
  82.      exit(0);
  83.       }
  84.  
  85.    adj = 0;                                /* adjacency reset to zero */
  86.  
  87.    for (i=0; i<n; i++)                     /* generate possible adj term */
  88.       {
  89.      for (j=0; j<nspm; j++)            /* loop for nspm>1 */
  90.         {
  91.            if ((i/8) == j)
  92.           *(adjterm+j) = *(temp+j) ^ (1<<(i%8));   /* XOR */
  93.            else
  94.           *(adjterm+j) = *(temp+j);
  95.         }
  96.  
  97.      test = exist(adjterm, a, m);       /* check for existence in a-array */
  98.      if (test == 0)                     /* exist in array a, an adj term */
  99.         {
  100.            adj++;                       /* increment adjacency */
  101.            if (adj > limit)             /* adjacency > limit */
  102.           break;
  103.            c = (unsigned char *) realloc(c, 4+nspm*(adj+1));  /* more space */
  104.            if (c == 0)
  105.           {
  106.              printf("Out of memory -- ADJACENT, *c\n");
  107.              printf("Program terminated - 3\n");
  108.              exit(0);
  109.           }
  110.  
  111.            memcpy((c+4+nspm*adj), adjterm, nspm);  /* add adjacent term to c array */
  112.         }
  113.       }
  114.  
  115.    *(c+1) = adj;                      /* update adjacency to c array */
  116.    *(c+2) = 0;                        /* for use in reduce function */
  117.  
  118.    free(adjterm);                     /* free pointer */
  119.  
  120.    return(c);                         /* return minterm plus adjacent terms */
  121. }
  122.  
  123.  
  124.